package com.zhangwoo.util;


/**
 * 报警实体类。包含验证报警的方法。
 * 
 * @author chenc@inetcop.com.cn
 * @date 2009-08-10
 * 
 */
public class Alarm {
	/**
	 * 自身检查是否报警方法
	 * @param content
	 * @return
	 */
	public static boolean isAlerm(String alermexp,String content){
		if(alermexp==null||alermexp.trim().length()==0) return false;
		
		String exps=fixExp(alermexp);// 所有关键字
		if(content==null||content.trim().length()==0)
			return false;
		return expAlarm(exps,content);
	}
	
	private static String fixExp(String exp) {
		// 判断表达式是否正确，只能包含一重括号 只判断一次
		boolean fix = true;
		int iCount = 0;
		int length = exp.length();
		for (int i = 0; i < length; i++) {
			char cc = exp.charAt(i);
			if (cc == '(')
				iCount++;
			else if (cc == ')')
				iCount--;

			if (iCount < 0 || iCount > 1) {
				fix = false;
				break;
			}
		}

		// 表达式错误
		if (!fix || iCount != 0) {
			return "";
		}

		exp = exp.replaceAll("　", "");// 全角空格
		
		exp=exp.replaceAll("\\s+", " ");
		exp=exp.replace(" and ", "and")
			.replace(" or ", "or")
			.replace(" not ", "not");
//
//		exp = exp.replaceAll(" ", "");// 半角空格

		exp = exp.replaceAll("[\\|\\+,\\-,\\^,']", "");// 去掉特殊字符

		exp = exp.replaceAll("(?i)and", "|'+");
		exp = exp.replaceAll("(?i)or", "|'-");
		exp = exp.replaceAll("(?i)not", "|'^");

		if (!exp.startsWith("|"))
			exp = "|'+" + exp;

		String exp1 = "";
		int i1 = 0;
		int i2 = 0;
		length = exp.length();

		for (int i = 0; i < length; i++) {
			char cc = exp.charAt(i);
			if (cc == '(')
				iCount++;

			if (iCount == 1) {
				i2 = exp.indexOf(")", i);
				String s1 = exp.substring(i1, i + 1);
				String s2 = exp.substring(i + 1, i2);
				s2 = s2.replaceAll("\\|'", "~");
				exp1 = exp1 + s1 + s2;
				i1 = i2;
				i = i2 + 1;
				iCount = 0;
			}
		}

		exp1 = exp1 + exp.substring(i2);
		exp = exp1;

		// 将括号里面的分隔符合括号外面分开,以便将括号当成一个整体 |'+(搜索~+技巧~^你好)|'+基本|'^合适

		String[] expL = exp.split("\\|");

		String NOT = "", AND = "", OR = "";

		for (int i = 0; i < expL.length; i++) {
			if (expL[i].startsWith("'+")) {
				AND = AND + expL[i];
			} else if (expL[i].startsWith("'^")) {
				NOT = NOT + expL[i];
			} else if (expL[i].startsWith("'-")) {
				OR = OR + expL[i];
			}
		}

		exp = NOT + OR + AND;

		return exp;

	}

	private static boolean expAlarm(String exp, String text) {
		if (exp.equals(""))
			return false;
		exp = exp.substring(1);
		String[] expL = exp.split("'");

		for (int i = 0; i < expL.length; i++) {// 优先级，一不符合，就退出，最后不符合退出的条件，则为真
			if (expL[i].startsWith("^")) {// 匹配 NOT
				if (aExpAlarm(expL[i], text))
					return false;
			} else if (expL[i].startsWith("-")) {
				if (aExpAlarm(expL[i], text))
					return true;
			} else if (expL[i].startsWith("+")) {
				if (!aExpAlarm(expL[i], text))
					return false;
			} else {
				return false;
			}
		}
		return true;
	}

	// 判断单个 正则表达式 或者字符串
	private static boolean aExpAlarm(String exp, String text) {
		boolean cat = false;

		exp = exp.substring(1);
		if (exp.indexOf("(") != -1 && exp.indexOf("*") != -1) {
			if (text.indexOf(exp) != -1)
				cat = true;
		} else if (exp.indexOf("(") != -1) {
			exp = exp.substring(1, exp.length() - 1);// 包含括号

			// /////////////////////重新正规化
			String[] expL = exp.split("~");

			String NOT = "", AND = "", OR = "";

			for (int i = 0; i < expL.length; i++) {
				if (expL[i].startsWith("+")) {
					AND = AND + "'" + expL[i];
				} else if (expL[i].startsWith("^")) {
					NOT = NOT + "'" + expL[i];
				} else if (expL[i].startsWith("-")) {
					OR = OR + "'" + expL[i];
				} else {
					AND = AND + "'+" + expL[i];
				}
			}

			exp = NOT + OR + AND;
			return (expAlarm(exp, text));

		} else {
			exp = exp.replaceAll("\\*", "\\.");
			if (text.matches("(?s).*" + exp + ".*"))
//			if (JavaUtil.isAllMatch(text,".*" + exp + ".*"))
				cat = true;
		}
		return cat;
	}
}
